/**
 * Created by Administrator on 2018/5/18.
 */
define(function (require,exports,module) {
    var sTpl = require("./flightManager.html");
    require("./flightManager.css");
    var VueComponent = Vue.extend({
        template: sTpl,
        data :function () {
            return {
                url :basepath+"/api/flightInfo/query?filter=",
                formDateLoading:false,
                currentScope:'',
                currentEquip:{},
                formTitle:"添加",
                dialogMaintenanceFormVisible:false,
                maintenanceFormTitle:"添加",
                repairPlaneTitle:"添加",
                dialogRepairPlaneSettings:false,
                emptyMaintenancePlanSettingForm:{},
                reparePlaneForm:{},
                activeNames:['1','2'],
                currentIndex:'',
                queryForm:{
                    filter:"",
                    status:"",
                },
                slidingWindowVisible:false,
                add:true,
                formData:{

                },
                teams:[]
            }
        },
        mounted:function(){
             var $this = this;
            axios.get(basepath+"/api/webframe/fwteam/queryAllTeamsNopage").then(function (response) {
                $this.teams = response.data;
            });

        },
        methods: {

            getEmptyReparePlaneForm:function () {
              return{
                  leastedOverhaulDate:"",
                  middleCycle:"",
                  middleCycleDate:"",
                  minorRepairCycle:"",
                  minorRepairDate:"",
                  overhaulCycle:"",
                  unit:"月"
              }
            },
            getEmptyMaintenancePlanSettingForm:function () {
                return{
                    flight:true,
                    leastPlaneDate:"",
                    maintenanceContent:"",
                    cycle:12,
                    unit:'月'
                }

            },
            deleteEquip:function (scope) {
                var $this =this;
                this.$confirm('确定要删除设备?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+"/api/equipment/delete",scope.row).then(function (response) {
                        $this.$refs.table.internalData.splice(scope.$index,1);
                        $this.$notify({
                            title:"删除成功",
                            message:"设备数据已删除"
                        })
                    })
                });

            },

            handleChange:function(){
                return;
            },
            deleteReparePlanSetting:function (scope) {
                var $this =this;
                this.$confirm('确定要删除该配置？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+"/api/equipment/deleteRepairPlane",scope.row).then(function (response) {
                        $this.currentEquip.repairPlaneSettings.splice(scope.$index,1);
                        $this.$notify({
                            title:"删除成功",
                            message:"设备维修配置已删除"
                        })
                    })
                });

            },
            editReparePlaneSetting:function (scope) {
                this.dialogRepairPlaneSettings=true;
                scope.row.$index = scope.$index;
                this.reparePlaneForm = JSON.parse(JSON.stringify(scope.row));

            },
            addReparePlaneSetting:function () {
                this.dialogRepairPlaneSettings = true;
                this.reparePlaneForm= this.getEmptyReparePlaneForm()
            },
            confirmAddRepareSettingSetting:function () {
                var $this = this;
                $this.reparePlaneForm.flightInfoPo = {flightInfoId:$this.currentEquip.flightInfoId};

                // $this.reparePlaneForm.equipmentPo = {id:$this.currentEquip.id};
                this.dialogRepairPlaneSettings=false;
                axios.post(basepath+"/api/equipment/saveRepairPlane",this.reparePlaneForm).then(function (response) {
                    if($this.reparePlaneForm.id){
                        var index =$this.reparePlaneForm.$index;
                        $this.currentEquip.repairPlaneSettings[index]=response.data;
                        $this.$set($this.currentEquip.repairPlaneSettings,index,response.data);

                        $this.$notify({title:"修改成功",message:"维修计划配置修改成功"})
                    }else{
                        $this.currentEquip.repairPlaneSettings.unshift(response.data)
                        $this.$notify({title:"添加成功",message:"维修计划配置修录入成功"})
                    }
                })

            },
            deleteMaintenancePlanSetting:function (scope) {
                var $this =this;
                this.$confirm('确定要删除该配置？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+"/api/equipment/deleteMaintenancePlanSetting",scope.row).then(function (response) {
                        $this.currentEquip.maintenancePlanSettings.splice(scope.$index,1);
                        $this.$notify({
                            title:"删除成功",
                            message:"设备保养配置已删除"
                        })
                    })
                });

            },
            editMaintenanceSetting:function (scope) {
                this.dialogMaintenanceFormVisible=true;
                scope.row.$index = scope.$index;
                this.emptyMaintenancePlanSettingForm = JSON.parse(JSON.stringify(scope.row));

            },
            addMaintenanceSetting:function () {
                //this.$refs.maintenanceSetting.dialogFormVisible=true
                this.dialogMaintenanceFormVisible=true;
                this.emptyMaintenancePlanSettingForm = this.getEmptyMaintenancePlanSettingForm();
            },
            confirmAddMaintenanceSetting:function () {
                var $this = this;
                $this.emptyMaintenancePlanSettingForm.flightInfoPo = {flightInfoId:$this.currentEquip.flightInfoId};

                this.dialogMaintenanceFormVisible=false;
                axios.post(basepath+"/api/equipment/saveMaintenancePlanSetting",this.emptyMaintenancePlanSettingForm).then(function (response) {
                    if($this.emptyMaintenancePlanSettingForm.id){
                        var index =$this.emptyMaintenancePlanSettingForm.$index;
                        $this.currentEquip.maintenancePlanSettings[index]=response.data;
                        $this.$set($this.currentEquip.maintenancePlanSettings,index,response.data);
                        //$this.$set($this.$refs.maintenanceSetting.$refs.elTable.data,index,response.data);
                        $this.$notify({title:"修改成功",message:"保养计划配置修改成功"})
                    }else{
                        $this.currentEquip.maintenancePlanSettings.unshift(response.data)
                        // $this.$refs.table.$refs.elTable.data.unshift(response.data);
                        // $this.$set( $this.$refs.table.$refs.elTable,'data',$this.$refs.table.$refs.elTable.data)
                        // $this.$refs.table.dialogFormVisible = false;
                        $this.$notify({title:"添加成功",message:"保养计划配置修录入成功"})
                    }
                })

            },
            pickerLeastedOverhaulDate:function (val) {
                this.reparePlaneForm.leastedOverhaulDate = val;
            },
            pickerMiddleCycleDate:function (val) {
                this.reparePlaneForm.middleCycleDate = val;
            } ,
            pickerMinorRepairDate:function (val) {
              this.reparePlaneForm.minorRepairDate= val;
            },
            pickerLeastPlaneDate:function(val){
                this.emptyMaintenancePlanSettingForm.leastPlaneDate = val;
            },
            editRM:function (scope) {
                this.slidingWindowVisible=true;
                this.currentEquip= this.$refs.table.$refs.elTable.data[scope.$index] ;
                if(!this.currentEquip.maintenancePlanSettings){
                    this.currentEquip.maintenancePlanSettings=[];
                }
                if(!this.currentEquip.repairPlaneSettings){
                    this.currentEquip.repairPlaneSettings=[];
                }
            },
            selectRow:function (row,index) {
                this.currentIndex = row.flightInfoId;
            },
            getEmptyFormData:function () {
              return {
                  equipmentName:"",
                  equipmentNum:"",
                  manufacturingNo:"",
                  makeDate:"",
                  useDate:"",
                  specifications:"",
                  teamName:"",
                  teamId:"",
                  status:""
              }

            },
            teamIdConfirm:function (team) {
                this.formData.teamId = team.teamId;
            },
            pickerMakeDate:function (val) {
                this.formData.makeDate= val;
            },
            query:function () {
               this.url =basepath+"/api/flightInfo/query?filter="+this.queryForm.filter+"&status="+this.queryForm.status
                // this.$refs.queryForm.resetFields();
            },
            addEqupment:function () {
                this.formTitle="添加"
                this.formData =this.getEmptyFormData();
                this.add=true;
                this.$refs.table.dialogFormVisible = true
            },
            pickerUseDate:function (val) {
                this.formData.useDate =val;

            },
            editRow:function(scope){
                this.formTitle="编辑"
                this.formData =JSON.parse(JSON.stringify(scope.row));
                this.currentScope = scope;
                this.$refs.table.dialogFormVisible = true
                this.add=false;
            },
            saveEquipment:function (currentScope) {
                var $this = this;
                axios.post(basepath+"/api/equipment/save",this.formData).then(function (response) {
                    if(!$this.add){
                        $this.$set( $this.$refs.table.$refs.elTable.data,currentScope.$index,response.data)
                        $this.$refs.table.dialogFormVisible = false;
                        $this.$notify({title:"修改成功",message:"设备信息修改成功"})
                    }else{
                        $this.$refs.table.$refs.elTable.data.unshift(response.data);
                        $this.$set( $this.$refs.table.$refs.elTable,'data',$this.$refs.table.$refs.elTable.data)
                        $this.$refs.table.dialogFormVisible = false;
                        $this.$notify({title:"添加成功",message:"设备信息录入成功"})
                    }
                })
            },
            cancelSave:function () {
                this.$refs.table.dialogFormVisible = false
            }
        },
        watch:{

        },
        activated:function (){
            messages.$emit('change','航空器维养配置');
        }
    });
    module.exports = VueComponent;
})
